package com.ece.mediatheque.basededonnee;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;

/**
 * Created by loutry on 10/27/13.
 */
public class DatabaseProvider extends ContentProvider {
    public static final String CONTENT = "content://";
    public static final String SLASH = "/";

    private DBHelper dbHelper;
    private static UriMatcher sUriMatcher;
    private static final String AUTHORITY = ".db.DatabaseProvider";

    public static enum TABLES {
        PLAT(DBHelper.TABLE_PLAT),
        RECETTE(DBHelper.TABLE_RECETTE),
        INGREDIENT(DBHelper.TABLE_INGREDIENT),
        MENU(DBHelper.TABLE_MENU),
        MENU_COMPOSITION(DBHelper.TABLE_MENU_COMPOSITION),
        SEASON_PLAT(DBHelper.TABLE_SEASON_PLAT),
        SEASON(DBHelper.TABLE_SEASON),
        HORAIRE_PLAT(DBHelper.TABLE_HORAIRE_PLAT),
        HORAIRE(DBHelper.TABLE_HORAIRE),
        COMMENT(DBHelper.TABLE_COMMENT),
        CONSOMMATEUR(DBHelper.TABLE_CONSOMMATEUR);

        public String table;

        private TABLES(String table) {
            this.table = table;
        }
    };

    @Override
    public boolean onCreate() {
        dbHelper = new DBHelper(getContext());

        if(sUriMatcher == null)
        {
            sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
            for(int i = 0; i < TABLES.values().length; i++)
            {
                TABLES table = TABLES.values()[i];
                sUriMatcher.addURI(getAuthority(getContext()), table.table, i);
            }
        }
        return true;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
        try{
            SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
            qb.setTables(TABLES.values()[sUriMatcher.match(uri)].table);

            SQLiteDatabase db = dbHelper.getReadableDatabase();
            Cursor c = qb.query(db, projection, selection, selectionArgs, null, null, sortOrder);
            c.setNotificationUri(getContext().getContentResolver(), uri);
            return c;
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public String getType(Uri uri) {
        final String type;
        type = TABLES.values()[sUriMatcher.match(uri)].table;

        return type;
    }

    @Override
    public Uri insert(Uri uri, ContentValues initialValues) {
        try{
            ContentValues values;
            if (initialValues != null) {
                values = new ContentValues(initialValues);
            } else {
                values = new ContentValues();
            }

            SQLiteDatabase db = dbHelper.getWritableDatabase();

            String table = TABLES.values()[sUriMatcher.match(uri)].table;

            long rowId = db.replace(table, null, values);
            if (rowId > 0) {
                Uri noteUri = ContentUris.withAppendedId(
                        Uri.parse(CONTENT + AUTHORITY + SLASH + table), rowId);
                getContext().getContentResolver().notifyChange(noteUri, null);
                return noteUri;
            }
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int delete(Uri uri, String where, String[] whereArgs) {
        try{
            SQLiteDatabase db = dbHelper.getWritableDatabase();
            int count;
            count = db.delete(TABLES.values()[sUriMatcher.match(uri)].table, where, whereArgs);
            getContext().getContentResolver().notifyChange(uri, null);
            return count;
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int update(Uri uri, ContentValues contentValues, String where, String[] whereArgs) {
        try{
            SQLiteDatabase db = dbHelper.getWritableDatabase();
            int count = db.update(TABLES.values()[sUriMatcher.match(uri)].table, contentValues, where, whereArgs);

            getContext().getContentResolver().notifyChange(uri, null);
            return count;
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        return 0;
    }

    public static String getAuthority(Context context)
    {
        return context.getPackageName() + AUTHORITY;
    }
}
